let request = require('operation.js')
let klassModel = require('../models/klass.js')
let noticeModel = require('../models/notice.js')

module.exports.getClassNotices = getClassNotices
module.exports.getNotices = getNotices

/**
 * 获取通知
 * params: 
 *    type == 'created'：查询自己创建的所有通知
      type == 'received'：查询用户所属所有班级收到的所有通知
 */
function getNotices(noticeType) {
    return new Promise((resolve, reject) => {
      request.getRequest('/v1/notices?type=' + noticeType, {}, true)
        .then(res => {
          wx.stopPullDownRefresh()

          var data = res.data;
          if (request.SUCCESSED == data.status) {
            if ('received' == noticeType) {
              noticeModel.setClassNotices(data.items)
            }

            resolve(data.items)
          } else {
            wx.showToast({
              title: data.msg,
              iocn: 'none'
            })
          }
        })
        .catch(e => {
          console.log(e)
        })
    })
}


/**
 * 获取班级通知
 */
function getClassNotices() {
  var klass = klassModel.getCurrentClass();
  if (klass) {
    return new Promise((resolve, reject) => {
      request.getRequest('/notices?klassSid=' + klass.sid, {}, true)
        .then(res => {
          wx.stopPullDownRefresh()

          var data = res.data;
          if (request.SUCCESSED == data.status) {
            noticeModel.setClassNotices(data.items)
            resolve(data.items)
          } else {
            wx.showToast({
              title: data.msg,
              iocn: 'none'
            })
          }
        })
        .catch(e => {
          console.log(e)
        })
    })
  }
}

